<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * banner管理
 */
class Banner extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'banner';




    /**
     * 获取banner列表
     * @param page 页数
     * @param limit int 分页大小
     * @param is_play int 是否发布 1.发布 2.未发布    默认1
     * @param big_type_id 空则表示全显示  0 表示 只显示在推荐
     * @param keywords string 搜索关键词(文章标题)
     * @param skip_way 跳转方式  1、H5链接，2、每日荐读  3、阅读书单  4、资源详情  5 不跳转
     */
    public function lists($field = null, $keywords = null, $big_type_id = null, $is_play = null, $skip_way = null, $limit = 10)
    {
        if (empty($field)) {
            $field = ['id', 'is_play', 'name', 'img', 'link', 'big_type_id', 'sort', 'intro', 'create_time', 'skip_way'];
        }
        $res = $this->select($field)
            ->where(function ($query) use ($keywords) {
                if ($keywords) {
                    $query->where('name', 'like', '%' . $keywords . '%');
                }
            })->where(function ($query) use ($big_type_id, $is_play, $skip_way) {
                //特殊处理全显示的,0表示推荐
                if (isset($big_type_id)) {
                    $query->where('big_type_id', $big_type_id);
                }
                if ($is_play) {
                    $query->where('is_play', $is_play);
                }
                if ($skip_way) {
                    $query->where('skip_way', $skip_way);
                }
            })
            ->where('is_del', 1)
            ->orderByDesc('sort')
            ->orderBy('id')
            ->paginate($limit)
            ->toArray();
        if ($res['data']) {
            foreach ($res['data'] as $key => $val) {
                $res['data'][$key]['big_type_name'] = ResourceType::where('id', $val['big_type_id'])->where('is_del', 1)->value('type_name');
                $res['data'][$key]['link_name'] = $this->getResourceInfo($val['skip_way'], $val['link']);
            }
        }
        return $res;
    }

    /**
     * banner详情
     * @param id
     */
    public function detail($id)
    {
        $field = ['id', 'is_play', 'name', 'img', 'link', 'big_type_id', 'sort', 'intro', 'create_time', 'skip_way'];
        $res = $this->select($field)
            ->where('id', $id)
            ->where('is_del', 1)
            ->first();
        if ($res) {
            $res = $res->toArray();
            $res['big_type_name'] = ResourceType::where('id', $res['big_type_id'])->where('is_del', 1)->value('type_name');
            $res['link_name'] = $this->getResourceInfo($res['skip_way'], $res['link']);
        }
        return $res;
    }

    /**
     * 获取资源对应信息
     * skip_way 跳转方式  1、H5链接，2、每日荐读  3、阅读书单  4、资源详情 5、不跳转
     */
    public function getResourceInfo($skip_way, $link)
    {
        switch ($skip_way) {
            case '1':
                $name = $link;
                break;
            case '2':
                $name = ResourceReadRecom::where('id', $link)->where('is_del', 1)->value('name');
                break;
            case '3':
                $name = ResourceReadingList::where('id', $link)->where('is_del', 1)->value('name');
                break;
            case '4':
                $name = ResourceTotal::where('guid', $link)->where('is_del', 1)->value('title');
                break;
            default:
                $name = '';
        }
        return $name;
    }
}
